import { http } from "@/utils/http";
import md5 from "md5";

export class UserResult {
  avatar: string;
  /** 用户名 */
  userName: string;
  token: string;
}

export class UserInfoResult {
  avatar: string;
  id: number;
  loginName: string;
  nick_name: string;
  isMyPasswd: number;
  mobile: string;
  lastTime: string;
  createdAt: string;
  status: number;
}

/** 登录 */
export const loginPwd = (d?: any) => {
  const data = Object.assign({}, d);
  data.password = md5(data.password);
  return http.request<UserResult>(
    "post",
    "/admin/admin/admin/login",
    { data },
    UserResult
  );
};
/** 退出登录 */
export const logOut = () => {
  return http.request("get", "/admin/admin/admin/logout");
};
/** 登录用户详情 */
export const userInfoData = () => {
  return http.request<UserInfoResult>(
    "post",
    "/admin/admin/admin/info",
    null,
    UserInfoResult
  );
};
/** 管理员列表
 * @param loginName mobile status
 */
export const adminList = (data?: any) => {
  return http.request("post", "/admin/admin/admin/list", { data });
};
/**
 * 添加管理员
 * @param loginName userName password mobile roleId avatar status
 */
export const addAdminPost = (data?: any) => {
  return http.request("post", "/admin/admin/admin/add", { data });
};
/**
 * 检查管理员
 */
export const checkAdmin = (loginName: string) => {
  return http.request("post", "/admin/admin/admin/check", {
    data: { loginName }
  });
};
/**
 * 修改管理员自己的密码
 * @param oldPwd newPwd
 */
export const changeSelfPwd = (d: any) => {
  const data = Object.assign({}, d);
  data.oldPwd = md5(data.oldPwd);
  data.newPwd = md5(data.newPwd);
  return http.request("post", "/admin/admin/set/pwd", {
    data
  });
};

/**
 * 重置指定管理员的密码 为hanluo2025@123
 */
export const resetPwd = (aId: number) => {
  return http.request("post", "/admin/admin/initial/pwd", {
    data: { aId }
  });
};
// 根据手机号查询用户
export const getInfo = (mobile: string) => {
  return http.request("post", "/admin/user/moblie/info", {
    data: { mobile }
  });
};
// 获取用户等级列表
export const getLevels = () => {
  return http.request("post", "/admin/user/level/list");
};
// 修改等级
export const setLevel = (data?: any) => {
  return http.request("post", "/admin/user/level/set", { data });
};
